﻿/************************************************************************************
 * xujiajun@yoozoo.com 御剑
 * LOD管理器
 ***********************************************************************************/

using System.Collections.Generic;
using Pathfinding;
using UnityEngine.Profiling;

namespace WorldMapBase.LOD
{
    /// <summary>
    /// LOD管理器
    /// </summary>
    public class LODManager
    {
        /// <summary>
        /// 所有LOD策略的列表
        /// </summary>
        private List<LODStrategy> m_StrategyList = new List<LODStrategy>();

        /// <summary>
        /// 所有LOD策略的数量
        /// </summary>
        private int m_StrategyCnt;

        /// <summary>
        /// 用于生成策略ID的计数
        /// </summary>
        private int m_StrategyIndex = 0;

        /// <summary>
        /// 添加指定LOD策略，返回策略ID。添加失败则返回-1
        /// </summary>
        /// <param name="strategy">LOD策略对象</param>
        /// <returns>策略ID</returns>
        public int AddLODStrategy(LODStrategy strategy)
        {
            if (strategy != null)
            {
                m_StrategyList.Add(strategy);
                m_StrategyCnt++;
                m_StrategyIndex++;
                strategy.id = m_StrategyIndex;
                strategy.Create();
                return m_StrategyIndex;
            }
            else
            {
                return -1;
            }
        }

        /// <summary>
        /// 移除指定LOD策略
        /// </summary>
        /// <param name="strategy"></param>
        public void RemoveLODStrategy(LODStrategy strategy)
        {
            if (strategy != null)
            {
                strategy.Destroy();
                m_StrategyList.Remove(strategy);
                m_StrategyCnt--;
            }
        }

        /// <summary>
        /// 清空销毁所有策略
        /// </summary>
        public void Clear()
        {
            for (int i = 0; i < m_StrategyCnt; i++)
            {
                m_StrategyList[i].Destroy();
            }
            m_StrategyList.Clear();
            m_StrategyCnt = 0;
        }

        /// <summary>
        /// 获取指定ID的策略
        /// </summary>
        /// <param name="id">策略ID</param>
        /// <returns>LOD策略对象</returns>
        public LODStrategy GetStrategy(int id)
        {
            for (int i = 0; i < m_StrategyCnt; i++)
            {
                var s = m_StrategyList[i];
                if (s.id == id)
                {
                    return s;
                }
            }
            return null;
        }


        private string[] sampleNames = new[]
        {
            "Strategy1.Update",
            "Strategy2.Update",
            "Strategy3.Update",
            "Strategy4.Update",
            "Strategy5.Update",
            "Strategy6.Update",
            "Strategy7.Update",
            "Strategy8.Update",
            "Strategy9.Update",
            "Strategy10.Update",
        };

        /// <summary>
        /// 轮询更新逻辑
        /// </summary>
        public void Update()
        {
            for (int i = 0; i < m_StrategyCnt; i++)
            {
                Profiler.BeginSample(sampleNames[i]);
                m_StrategyList[i].Update();
                Profiler.EndSample();
            }
        }
    }

}
